//
//  AccountBindDetialVC.swift
//  LeGou
//
//  Created by lxm on 2022/8/1.
//

import UIKit

class AccountBindDetialVC: ZMViewController {

    //MARK:--------------------属性--------------------
    
    // 0 微信 1 支付宝
    var type = 0
    
    var info : MineModels.WalletInfo!
    
    var nickname = ""
    
    var wxOpenId = ""
    //MARK:--------------------UI控件--------------------
    @IBOutlet weak var name: UILabel!
    
    @IBOutlet weak var icon: UIImageView!
    
    @IBOutlet weak var accountType: UILabel!
    
    @IBOutlet weak var tips: UILabel!
    
    //MARK:--------------------生命周期--------------------
    override func viewDidLoad() {
        super.viewDidLoad()
        setUI()
        addConstraints()
        bindAction()
    }
    //MARK:--------------------UI--------------------
    func setUI() {
        title = "已绑定账户"
        name.text = nickname
        icon.image = UIImage.init(named: type == 0 ? "pay_tool_wechat" : "pay_tool_alipay")
        accountType.text = type == 0 ? "微信宝账户" : "支付宝账户"
        tips.text = type == 0 ? "绑定微信账号" : "绑定支付宝账号"
    }
    
    func addConstraints() {
        
    }
    
    //MARK:--------------------methods--------------------
    func bindAction() {
        
    }
    
    @IBAction func onUnbind(_ sender: Any) {
        if !ZMAppShareData.share.safeUserInfo.isTradeOk {
            // 判断是否设置交易密码
            ZMAlertView.show(content: "还未设置交易密码", left: "关闭", right: "马上去设置") {
                
            } onRight: {
                let cls = Swift.type(of: self)
                MineService.shared.tradeSuccessToVC = cls
                let vc = TradeFroget()
                self.navigationController?.pushViewController(vc, animated: true)
            }
            return
        }
        
        ZMAlertView.show(title: "解除绑定", content: "解绑后会影响余额提现，是否确认解绑？", left: "取消", right: "确认") {
            
        } onRight: {
            let vc = PsdTradeCodeVerify()
            vc.onComplete = { [weak self] pwd in
                guard let self = self else {return}
                if self.type == 0 {
                    self.unbindWx(tradePwd: pwd)
                } else {
                    self.unbindAli(tradePwd: pwd)
                }
            }
            self.navigationController?.pushViewController(vc, animated: true)
        }
    }
    

    func unbindWx(tradePwd : String) {
        ZMToast.showActivity()
        MineAPI.wxUnbind(["tradePwd" : tradePwd]).rxRequest(type: Any.self).subscribe(onNext:{ [unowned self] resp in
            ZMToast.hiddenActivity()
            ZMAppShareData.share.requestUserInfo { ok in
                ZMToast.showToast(title: "已解绑")
                ZMNotification.shard.subject.onNext(.bindThirdSuccess)
                _ = ZMJumpHelper.jumpToClass(VC: self, toClass: ThirdAccountVC.classForCoder(), notContainAutoJump: true)
            }
        }).disposed(by: disposeBag)
    }
    
    func unbindAli(tradePwd : String) {
        ZMToast.showActivity()
        MineAPI.aliUnbind(["tradePwd" : tradePwd]).rxRequest(type: Any.self).subscribe(onNext:{ [unowned self] resp in
            ZMToast.hiddenActivity()
            ZMAppShareData.share.requestUserInfo { ok in
                ZMToast.showToast(title: "已解绑")
                ZMNotification.shard.subject.onNext(.bindThirdSuccess)
                _ = ZMJumpHelper.jumpToClass(VC: self, toClass: ThirdAccountVC.classForCoder(), notContainAutoJump: true)
            }
        }).disposed(by: disposeBag)
    }
    //MARK:--------------------delegate--------------------

}
